import java.util.Arrays;

/**
 * @Author @fiber-cloud
 * @Date 2025/6/22 11:24
 * JavaSE（9）和JavaSE（10）的作业
 */
public class Job {
    public static void main(String[] args) {
        int[] array = {1,2,3};
       // transform(array);
        System.out.println(Arrays.toString(array));

        System.out.println(avg(array));
        fun(array);
        System.out.println(Arrays.toString(array));

        System.out.println(fun2(array,2));

        int[] nums = {2,7,11,15};
        int[] res = fun3(nums,9);
        System.out.println(Arrays.toString(res));


        int[] arrat1 = {4,1,2,1,2};
        System.out.println(fun4(arrat1));


        int[] arrat2 = {2,2,1,1,1,2,2};
        System.out.println(fun5(arrat2));

        //没有三个连续奇数的情况
        int[] arrat3 = {2,6,3,2,3,4};
        System.out.println(fun6(arrat3));
        //有三个奇数的情况
        int[] arrat4 = {1,2,34,3,4,5,7,23,12};
        System.out.println(fun6(arrat4));
    }
    //改变原有数组元素的值
    public static void transform(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i]*2;
        }
    }

    //求数组的平均值
    public static float avg(int[] array) {
        float ret = 0;
        int sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        ret = (float) sum / array.length;
        return ret;
    }

    //奇数位于偶数之前
    public static void fun(int[] array){
        //使用两个下标，一个从0开始，一个从最后一个下标开始
        int left = 0;
        int right = array.length-1;


        while (left < right){
            while (left < right && array[left]%2 !=0){
                left ++;
            }
            //i下标为偶数
            while (left < right && array[right]%2 ==0){
                right -- ;
            }
            int tmp = array[left];
            array[left] = array[right];
            array[right] = tmp;
        }
    }

    //二分查找
    public static boolean fun2(int[] array,int i){
        //左边右边两个下标
        int left = 0;
        int right = array.length-1;
        Arrays.sort(array);
        while (left <=  right){
            int mid = (left + right)/2;
            if (array[mid] > i){
                right = mid -1;
            } else if (array[mid] < i) {
                left = mid + 1;
            }else{
                return true;
            }
        }
        return false;
    }


    //两数之和
    //给定一个整数数组 nums 和一个整数目标值 target.
    // 请你在该数组中找出 和为目标值 target 的那 两个 整数，并返回它们的数组下标。
    public static int[] fun3(int[] nums,int target){
        int[] res = new int[2];
        //双指针暴力求解
        for (int i = 0; i < nums.length-1; i++) {
            for (int j = nums.length-1; j > i ; j--) {
                if (nums[i]+nums[j] == target){
                    res[0] = i;
                    res[1] = j;
                }
            }
        }
        return res;
    }


    //只出现一次的数字
    public static int fun4(int[] array){
        int ret = 0;
        for (int i : array){
            ret ^= i;
        }
        return ret;
    }


    //多数元素
    public static int fun5(int[] array){
        Arrays.sort(array);
        int length = array.length -1;
        int ret = length/2;
        return array[ret];
    }


    //存在连续三个奇数的数组
    public static boolean fun6(int[] array){
        //记录有几个奇数
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i]%2 != 0){
                //是奇数
                count++;
                if (count == 3){
                    //存在三个奇数
                    return true;
                }
            }else {
                //是偶数，count重置
                count = 0;
            }
        }
        return false;
    }
}
